import 'dart:math';

import 'package:binggo_bomb/page/cardInfo.dart';
import 'package:binggo_bomb/utils/localStorage.dart';
import 'package:flutter/material.dart';

class CardsListPage extends StatefulWidget {
  final int colorId;
  const CardsListPage({super.key, required this.colorId});

  @override
  State<CardsListPage> createState() => CardsListPageState();
}

class CardsListPageState extends State<CardsListPage> {

  List<String> tabsName = ["红色", "蓝色", "黄色", "紫色"];

  /// 临时存放当前颜色卡券号码
  Map nums = {};

  /// 卡片组件
  List<Widget> cards = [];

  List<Color> cardColors = [Colors.pinkAccent, Colors.lightBlueAccent, Colors.yellowAccent, Colors.purpleAccent];

  // Card编辑起始位置
  int editStartIndex = 0;

  // 号码种子
  // List<List<int>> cardNums = [
  //   [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],
  // ];

  int inputModel = 1;

  String noWinningNums = "";

  late TextEditingController controller;

  @override
  void initState() {
    super.initState();
    print("initState");
    controller = TextEditingController();
    getCardNums(widget.colorId);
    buildCards();
  }




  @override
  void didUpdateWidget(CardsListPage oldWidget) {
    // buildCards();
    print("didUpdateWidget");
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 200,
      child: SingleChildScrollView(
        child: Column(
          children: cards
        ),
      ),
    );
  }



  List<Widget> buildItem(List ids) {
    List<Widget> listWidgets = [];
    for(int i = 0;i < ids.length; i++) {
      listWidgets.add(
            Container(
              width: 20,
              height: 20,
              color: cardColors[widget.colorId],
              alignment: Alignment.center,
              child: Text( ids[i].toString(),
                style: TextStyle(
                    color: Colors.black,
                    fontSize: 24,
                    fontWeight: FontWeight.bold
                ),
              ),
            ),
      );
    }
    return listWidgets;
  }


  /// 获取卡片数据
  getCardNums(int color) {
    Map res = {};
    switch(color) {
      case 0: /// "红色"
        res = LocalStorage.getKey<Map>('pinkCards') != null ? LocalStorage.getKey<Map>('pinkCards') as Map : {};
        break;
      case 1: /// "蓝色"
        res = LocalStorage.getKey<Map>('blueCards') != null ? (LocalStorage.getKey<Map>('blueCards') as Map) : {};
        break;
      case 2: /// "黄色"
        res = LocalStorage.getKey<Map>('yellowCards') != null ? LocalStorage.getKey<Map>('yellowCards') as Map : {};
        break;
      case 3: /// "紫色"
        res = LocalStorage.getKey<Map>('purpleCards') != null ? LocalStorage.getKey<Map>('purpleCards') as Map : {};
        break;
      default: /// "红色"
        res = LocalStorage.getKey<Map>('pinkCards') != null ? LocalStorage.getKey<Map>('pinkCards') as Map : {};
        break;
    }
    setState(() {
      nums = res;
    });
  }


  /// 构建卡片组件
  buildCards () {

    int k = nums.length;
    List<Widget> ws = [];
    for(int i = 1;i <= k;i++) {

      ws.add(
          InkWell(
            child: Container(
              width: 260,
              height: 260,
              margin: EdgeInsets.only(top: 15, bottom: 15),
              child: Card(
                color: Colors.black,
                elevation: 4,
                shape: RoundedRectangleBorder(),
                child: Container(
                  margin: EdgeInsets.only(left: 2, top: 2,right: 2, bottom: 2),
                  child: GridView.count(
                    crossAxisCount: 5,
                    mainAxisSpacing: 2,
                    crossAxisSpacing: 2,
                    children: buildItem(nums[i.toString()]),
                  ),
                ),
              ),
            ),
            onTap: () {
              print("编辑第${i}张${tabsName[widget.colorId]}券");
              Navigator.of(context).push(
                  MaterialPageRoute(
                      builder: (_) =>
                          CardInfoPage(cardIndex: i, nums: nums[i.toString()], colorId: widget.colorId, onChang: (color, index, nums) {
                            childChange(color, index, nums);
                          })
                  )
              );
            },
          )
      );

    }

    setState(() {
      cards = ws;
    });

  }


  /// 编辑更新卡片数据信息
  childChange(color, index, nums) {
    if(color == 0) { // 粉色
      /// 重新加载页面粉色券卡片
      setState(() {
        this.nums[index.toString()] = nums;
      });
      /// 更新缓存 粉色券 号码
      LocalStorage.set<Map>('pinkCards', this.nums);
      /// 重新构建列表卡片数据
      buildCards();
    } else if(color == 1) { // 蓝色

    } else if(color == 2) { // 黄色

    } else if(color == 3) { // 紫色

    } else if(color == 4) {

    }



  }


  /// 添加卡片
  addCard() {
    print("addCard");
  }

}
